package me.sauce.folkpocket48.utils

import android.databinding.BindingAdapter
import android.support.design.widget.Snackbar
import android.support.v4.widget.SwipeRefreshLayout
import android.view.View
import me.sauce.folkpocket48.base.BaseViewModel
import me.sauce.folkpocket48.view.ScrollChildSwipeRefreshLayout

/**
 * @author sauceWu
 * @since 2018/1/2 10:38
 */
/**
 * Transforms static java function Snackbar.make() to an extension function on View.
 */
fun View.showSnackbar(snackbarText: String, timeLength: Int) {
    Snackbar.make(this, snackbarText, timeLength).show()
}


@set:BindingAdapter("visibleOrGone")
var View.visibleOrGone
    get() = visibility == View.VISIBLE
    set(boolean) {
        visibility = if (boolean) View.VISIBLE else View.GONE
    }

/**
 * Reloads the data when the pull-to-refresh is triggered.
 *
 * Creates the `android:onRefresh` for a [SwipeRefreshLayout].
 */
@BindingAdapter("android:onRefresh")
fun ScrollChildSwipeRefreshLayout.setSwipeRefreshLayoutOnRefreshListener(viewModel: BaseViewModel) {
    setOnRefreshListener { viewModel.loadData(true) }


}